/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.model.relations;

import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.basics.ValueNode;
import org.jfonia.connect5.relations.Equal;
import org.jfonia.connect5.relations.Relation;
import org.jfonia.model.ISequenceObserver;
import org.jfonia.model.Sequence;
import org.jfonia.model.elements.Clef;
import org.jfonia.model.elements.MusicalElement;
import org.jfonia.pitch.RankNodes;

/**
 *
 * @author Jannes Plyson
 */
public class ToneToMidiPitchRelation implements Relation, ISequenceObserver{
    private ValueNode<Integer> base40Rank;
    private RankNodes rankNodes;
    private MutableValueNode<Integer> midiPitch;
    private Sequence<Clef> clefSequence;
    private Equal<Integer> equalRelation;
    public ToneToMidiPitchRelation(MutableValueNode<Integer> base40Rank,
                                   MutableValueNode<Integer> midiPitch,
                                   Sequence<Clef> clefSequence){
        this.base40Rank = base40Rank;
        this.midiPitch = midiPitch;
        this.clefSequence = clefSequence;
        equalRelation = null;
        rankNodes = new RankNodes(base40Rank);
        switchOn();
    }

    public void switchOn(){
        if(clefSequence != null)
                clefSequence.addObserver(this);
        if(base40Rank.getValue() != null){ //tone is used as start value
            equalRelation = new Equal<Integer>(rankNodes.getMidiRankNode(), midiPitch);
        }else if(midiPitch.getValue() != null){
            
        }
        base40Rank.addObserver(this);
    }

    public void switchOff(){        
        base40Rank.removeObserver(this);
        if(equalRelation != null){
            equalRelation.switchOff();
            equalRelation = null;
        }
    }

    public void onNotify(Object source) {
        if(source == base40Rank){
            if(base40Rank == null){

            }else{

            }
        }
    }

    public void nodeAdded(Sequence sequence, MusicalElement musicalElement) {}

    public void nodeRemoved(Sequence sequence, MusicalElement musicalElement) {}

    public void nodeDifferenceChanged(Sequence sequence, MusicalElement musicalElement) {}

}
